package com.deuwise.system.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.deuwise.common.utils.Constant;
import com.deuwise.system.entity.SysMenu;
import com.deuwise.system.service.SysMenuService;
import com.deuwise.system.mapper.SysMenuMapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.deuwise.system.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Sysmenu 接口实现类
 *@author deuwise
 *@date 2018-11-06 14:15:04
 **/
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Autowired
    private SysUserService sysUserService;
    Logger logger= LoggerFactory.getLogger(SysMenuServiceImpl.class);

    @Override
    public List<SysMenu> queryListParentId(String menuId,String code) {
        return baseMapper.queryListParentId(menuId,code);
    }

    @Override
    public List<SysMenu> queryNotButtonList() {
        return baseMapper.queryNotButtonList();
    }

    @Override
    public Integer queryByName(String name, SysMenu sysMenu) {
        Wrapper<SysMenu> dictDoEntityWrapper=new EntityWrapper<>();
        dictDoEntityWrapper.eq("name",name);
        if(sysMenu!=null){
            if(StringUtils.isNotBlank(sysMenu.getId())) {
                dictDoEntityWrapper.ne("id", sysMenu.getId());
            }
            if(StringUtils.isNotBlank(sysMenu.getParentId())) {
                dictDoEntityWrapper.eq("parent_id", sysMenu.getParentId());
            }
            if(StringUtils.isNotBlank(sysMenu.getAppId())){
                dictDoEntityWrapper.eq("app_id", sysMenu.getAppId());
            }
        }
        return baseMapper.selectCount(dictDoEntityWrapper);
    }

    @Override
    public List<SysMenu> getUserMenuList(String userId,String appId) {
        //系统管理员，拥有最高权限
        if(userId.equals(Constant.SUPER_ADMIN)){
            return getAllMenuList(null,appId);
        }

        //用户菜单列表
        List<String> menuIdList = sysUserService.queryAllMenuId(userId,appId);
        return getAllMenuList(menuIdList,appId);
    }

    @Override
    public List<SysMenu> queryListParentId(String parentId, List<String> menuIdList,String code) {
        List<SysMenu> menuList = queryListParentId(parentId,code);
        if(menuIdList == null){
            return menuList;
        }

        List<SysMenu> userMenuList = new ArrayList<>();
        for(SysMenu menu : menuList){
            if(menuIdList.contains(menu.getId())){
                userMenuList.add(menu);
            }
        }
        return userMenuList;
    }


    /**
     * 获取所有菜单列表
     */
    private List<SysMenu> getAllMenuList(List<String> menuIdList,String code){
        //查询根菜单列表
        List<SysMenu> menuList = queryListParentId("0", menuIdList,code);
        //递归获取子菜单
        getMenuTreeList(menuList, menuIdList,code);

        return menuList;
    }

    /**
     * 递归
     */
    private List<SysMenu> getMenuTreeList(List<SysMenu> menuList, List<String> menuIdList,String code){
        List<SysMenu> subMenuList = new ArrayList<SysMenu>();
        for(SysMenu entity : menuList){
            //目录
            if(entity.getType() == Constant.MenuType.CATALOG.getValue()){
                entity.setList(getMenuTreeList(queryListParentId(entity.getId(), menuIdList,code), menuIdList,code));
            }
            subMenuList.add(entity);
        }

        return subMenuList;
    }
}